#include "OffLineRenderPCH.h"

#include "OR_ScenePrimitiveGeometry.h"
#include "OR_Intersection.h"

using namespace OffLineRender;
using namespace Utility;
//----------------------------------------------------------
//OR_ScenePrimitiveGeometry::OR_ScenePrimitiveGeometry()
//----------------------------------------------------------
UT_BBOX_AABB OR_ScenePrimitiveGeometry::GetWorldBound() const
{
	return m_pkGeometry->GetWorldBound();
}
//----------------------------------------------------------
bool OR_ScenePrimitiveGeometry::CanIntersect() const
{
	return m_pkGeometry->CanIntersect();
}
//----------------------------------------------------------
bool OR_ScenePrimitiveGeometry::Intersect(const UT_Ray &r, OR_Intersection *isect) const 
{
	float thit, rayEpsilon;
	if (!m_pkGeometry->Intersect(r, &thit, &rayEpsilon, &isect->m_kDifferentialGeometry))
		return false;
	
	isect->m_pkScenePrimitiveBase = this;
	isect->m_kWorldToObject = *(m_pkGeometry->m_pkWorldToObject);
	isect->m_kObjectToWorld = *(m_pkGeometry->m_pkObjectToWorld);
	isect->m_uiGeometryId	= m_pkGeometry->m_uiGeometryId;
	isect->m_uiPrimitiveId  = m_uiPrimitiveId;
	isect->m_fRayEpsilon	= rayEpsilon;
	r.maxt = thit;
	return true;
}
//----------------------------------------------------------
bool OR_ScenePrimitiveGeometry::IntersectP(const UT_Ray &r) const 
{
	return m_pkGeometry->IntersectP(r);
}

//----------------------------------------------------------
void OR_ScenePrimitiveGeometry::Refine(UT_TVector<UT_SmartPointer<OR_ScenePrimitiveBase> > &refined) const 
{
	UT_TVector<UT_SmartPointer<UT_GeometryBase> > r;
	m_pkGeometry->Refine(r);
	
	for (uint32_t i = 0; i < r.GetSize(); ++i)
	{
		OR_ScenePrimitiveGeometry *gp = MM_NEW OR_ScenePrimitiveGeometry(r.GetAt(i),
			material, areaLight);
		refined.AddLast(gp);
	}
}

//----------------------------------------------------------